﻿using ProjScan.Config;
using ProjScan.Config.GUIConfig;
using ProjScan.Modules;
using ProjScan.ScanGUI.Tools;
using ProjScan.Tools;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ProjScan.ScanGUI
{
    internal class RuleGUI
    {
        //
        // 摘要:
        //     显示详细的规则设置界面
        public bool isShowRuleSetting;

        public Module fixModule;

        //
        // 摘要:
        //     规则key
        public string ruleKey;

        //
        // 摘要:
        //     是否激活
        public bool checkEnabled;

        //
        // 摘要:
        //     启用修复
        public bool fixEnabled;

        //
        // 摘要:
        //     是否需要开启错误资源页面
        public bool hasSrcPath;

        //
        // 摘要:
        //     目标路径
        public List<ScanAssetsConfig> targetConfig;

        //
        // 摘要:
        //     忽略路径
        public List<ScanAssetsConfig> ignoreConfig;

        //
        // 摘要:
        //     是否单独设置目标资源目录
        public bool isSeparateDirSettingsEnabled;

        //
        // 摘要:
        //     规则中文名称
        public string ruleName;

        //
        // 摘要:
        //     详细描述
        public string description;

        //
        // 摘要:
        //     扩展信息
        public string extraInfo;

        //
        // 摘要:
        //     阈值
        public int thresholdCompare;

        //
        // 摘要:
        //     默认阈值
        public float[] thresholdDefault;

        //
        // 摘要:
        //     阈值描述
        public string[] thresholdDescription;

        //
        // 摘要:
        //     规则优先级
        public RulePriority RulePriority;

        //
        // 摘要:
        //     拥有单独配置 选项
        public bool hasSeparateTargetDirSettings;

        //
        // 摘要:
        //     拥有白名单
        public bool hasWhiteList;

        //
        // 摘要:
        //     是否拥有不符合规范文件的信息
        public bool hasfailedData;

        //
        // 摘要:
        //     是否拥有修改规则
        public bool hasFixRule;

        private ScanAssetsConfigTreeView targetConfigTreeView;

        private ScanAssetsConfigTreeView ignoreConfigTreeView;

        private Action OpenRuleSettingAction;

        public FailAssetsWindow failAssetsWindow;

        private bool hasThreshold;

        public RuleGUI()
        {
        }

        public RuleGUI(Action openRuleSettingAction)
        {
            OpenRuleSettingAction = openRuleSettingAction;
        }

        internal virtual void DrawGUI(float width)
        {
            GUITools.DrawBox(width, drawShadow: true);
            EditorGUILayout.BeginHorizontal();
            GUIStyle foldoutHeader = EditorStyles.foldoutHeader;
            isShowRuleSetting = EditorGUILayout.BeginFoldoutHeaderGroup(isShowRuleSetting, ruleName, foldoutHeader);
            if (FailedResultManager.Instance.dicFails.ContainsKey(ruleKey) && FailedResultManager.Instance.dicFails[ruleKey].Count > 0)
            {
                GUILayout.Label("*", Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.RuleErrorInfo), GUILayout.Width(12f));
            }

            DrawToggle();
            EditorGUILayout.EndHorizontal();
            if (isShowRuleSetting)
            {
                DrawRuleSettingInfo();
            }

            EditorGUILayout.EndFoldoutHeaderGroup();
            GUITools.EndVertical();
        }

        private (string, Color) GetPriorityInfo(RulePriority priority)
        {
            string item = string.Empty;
            Color item2 = default(Color);
            switch (priority)
            {
                case RulePriority.None:
                    item = "暂无";
                    item2 = new Color(1f, 0.968627453f, 0.9019608f);
                    break;
                case RulePriority.Custom:
                    item = "自定义";
                    item2 = new Color(1f, 0.6627451f, 0.254901975f);
                    break;
                case RulePriority.Middle:
                    item = "中";
                    item2 = new Color(0.972549f, 0.470588237f, 0.447058827f);
                    break;
                case RulePriority.High:
                    item = "高";
                    item2 = new Color(0.9019608f, 0.141176477f, 0.07058824f);
                    break;
            }

            return (item, item2);
        }

        public virtual void DrawRuleSettingInfo()
        {
            GUILayout.BeginHorizontal();
            GUILayout.Space(18f);
            GUILayout.BeginVertical();
            (string, Color) priorityInfo = GetPriorityInfo(RulePriority);
            string item = priorityInfo.Item1;
            Color item2 = priorityInfo.Item2;
            GUIStyle tStyle = Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.RuleInfo);
            GUIStyle gUIStyle = new GUIStyle(tStyle);
            gUIStyle.normal.textColor = item2;
            GUILayout.BeginHorizontal();
            GUILayout.Label("【优先级】: ", tStyle);
            GUILayout.Label(item, gUIStyle);
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
            GUILayout.Label($"【规则ID】: {ruleKey}", Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.RuleInfo));
            GUILayout.Label($"【说  明】: {description}", Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.RuleInfo));
            if (hasSeparateTargetDirSettings)
            {
                GUITools.DrawToggleButton("是否启用单独的目标资源配置", ref isSeparateDirSettingsEnabled, 240, "是", "否");
                if (isSeparateDirSettingsEnabled)
                {
                    if (targetConfigTreeView == null)
                    {
                        CreateTargetTreeView(ref targetConfig);
                    }

                    targetConfigTreeView.Draw();
                    if (ignoreConfigTreeView == null)
                    {
                        CreateIgnoreTreeView(ref ignoreConfig);
                    }

                    EditorGUILayout.Space();
                    ignoreConfigTreeView.Draw();
                }
            }

            if (hasfailedData || hasWhiteList)
            {
                GUITools.DrawBox(bool_0: true);
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.Space();
                if (!hasSrcPath)
                {
                    if (GUILayout.Button("修复此规则", Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.button), GUILayout.Width(400f)))
                    {
                        FailedResultManager.Instance.LoadFailedData(ruleKey);
                        if (EditorUtility.DisplayDialog("修复资源", "该功能将修改您项目中的资源，请确认是否继续", "开始修复", "取消"))
                        {
                            ModulesManager.SingleFix(fixModule, ruleKey);
                        }
                    }
                }
                else if (GUILayout.Button("开启错误资源页面", Singleton<GUIStyleManager>.Instance.GetBasicStyle(TBasicStyle.button), GUILayout.Width(400f)))
                {
                    Debug.Log("打开错误资源页");
                    if (failAssetsWindow != null)
                    {
                        FailedResultManager.Instance.LoadFailedData(ruleKey);
                        failAssetsWindow.init(fixModule, hasfailedData, hasFixRule, hasWhiteList, hasSrcPath, ruleName, ruleKey);
                        failAssetsWindow.Show();
                        failAssetsWindow.Focus();
                    }
                    else
                    {
                        FailedResultManager.Instance.LoadFailedData(ruleKey);
                        failAssetsWindow = ScriptableObject.CreateInstance<FailAssetsWindow>();
                        failAssetsWindow.init(fixModule, hasfailedData, hasFixRule, hasWhiteList, hasSrcPath, ruleName, ruleKey);
                        failAssetsWindow.minSize = new Vector2(570f, 650f);
                        failAssetsWindow.Show();
                    }
                }

                EditorGUILayout.EndHorizontal();
                GUITools.EndVertical();
            }

            OpenRuleSettingAction?.Invoke();
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
        }

        public void CreateTargetTreeView(ref List<ScanAssetsConfig> lists)
        {
            if (thresholdDefault == null)
            {
                hasThreshold = false;
            }
            else
            {
                hasThreshold = true;
            }

            if (targetConfigTreeView == null)
            {
                targetConfigTreeView = ScanAssetsConfigTreeView.Create(ruleKey + "-Target", "目标资源配置", hasThreshold, thresholdDefault, thresholdDescription, thresholdCompare, ref lists, new GUIContent("目标资源", "可使用正则匹配"), "Assets");
            }
        }

        public void CreateIgnoreTreeView(ref List<ScanAssetsConfig> lists)
        {
            if (thresholdDefault == null)
            {
                hasThreshold = false;
            }
            else
            {
                hasThreshold = true;
            }

            if (ignoreConfigTreeView == null)
            {
                ignoreConfigTreeView = ScanAssetsConfigTreeView.Create(ruleKey + "-Ignore", "忽略资源配置", hasDefaultValue: false, null, null, 0, ref lists, new GUIContent("忽略资源", "可使用正则匹配"), "");
                ignoreConfigTreeView.minConfigCountToggle = false;
                ignoreConfigTreeView.isLimitEnabled = false;
            }
        }

        public virtual void DrawToggle()
        {
            if (hasFixRule)
            {
                GUIStyle style = fixEnabled ? Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.ButtonEnable) : Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.ButtonDisable);
                fixEnabled = GUILayout.Toggle(fixEnabled, fixEnabled ? "修复已启用" : "修复已关闭", style);
            }
            else
            {
                fixEnabled = false;
            }

            GUILayout.Space(10f);
            GUIStyle style2 = checkEnabled ? Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.ButtonEnable) : Singleton<GUIStyleManager>.Instance.GetTStyle(TStyle.ButtonDisable);
            checkEnabled = GUILayout.Toggle(checkEnabled, checkEnabled ? "检测已启用" : "检测已关闭", style2);
        }
    }
}